define(['vue','layer'],function (Vue,layer){
    'use strict';
    const mufiles = {
        template:`<div class="flex-row flex-item-center flex-content-center">
    <el-input type="text" v-model="showStr" @input="changeUrl" style="width: 240px"></el-input>
<el-upload
                                class="avatar-uploader"
                                :action="uploadurl"
                                :show-file-list="false"
                                :on-success="handleFileSuccess"
                                :on-remove="handleRemove"
                        >
                            <el-button type="primary" style="margin-left: 5px;" color="#626aef">上传</el-button>
                        </el-upload><el-button style="margin-left: 5px;margin-top: -2px" type="primary" color="#626aef" @click="handleShowAttachment">选择文件</el-button></div>`,
        props:{
            modelValue: { type:Array||String, default: [] },
        },
        emits: ['update:modelValue'],
        data(){
            return {
                uploadurl: Config.upload.uploadurl
            }
        },
        setup(props, { emit }) {
            const showStr = Vue.ref('')
            const showList = Vue.ref([])
            if(Array.isArray(props.modelValue)){
                showList.value = props.modelValue.map(item=>{
                    return {
                        url:item,
                        name:item
                    }
                })
            }else{
                let strArr = props.modelValue.split(',')
                strArr.forEach(item=>{
                    showList.value.push({
                        url:item,
                        name:item
                    })
                })
            }
            Vue.watch(()=>props.modelValue,(newvalue)=>{
                console.log(newvalue)
                if(newvalue){
                    if(Array.isArray(newvalue)){
                        showList.value = newvalue
                        showStr.value = newvalue.join(',')
                    }else{
                        showList.value = [newvalue]
                        showStr.value = newvalue
                    }

                }else{
                    showList.value = []
                    showStr.value = ''
                }
            })
            const updateValue = (resout=false)=>{
                if(resout===false){
                    let strs = []

                    showList.value.forEach(item=>{
                        strs.push(item.url)
                    })
                    showStr.value = strs.join(',')
                }else{
                    if(showStr.value==''){
                        showList.value = [];
                    }else{
                        showList.value = showStr.value.split(',')
                    }
                }
                console.log(showList.value,showStr.value,resout)
                let value = showList.value.map(item=>item.url)

                emit('update:modelValue',value)

            }
            updateValue()
            const changeUrl = (value)=>{
                showStr.value = value

                updateValue(true)
            }
            const handleFileSuccess = (response,uploadFile)=>{
                let url = response.data.url
                if(url){
                    // console.log(showList.value)
                    showList.value.push({
                        url:url,
                        name:url
                    })
                    updateValue()
                }
            }
            const handleRemove = (uploadFile, uploadFiles)=>{

            }
            const handleShowAttachment = (res)=>{
                let url = Config.domain+Config.attachment_url+`?select=1&multiple=1`
                let option = {
                    callback:function (res){
                        res.forEach(item=>{
                            showList.value.push({
                                url:item,
                                name:item
                            })
                        })
                        updateValue()
                    }
                }
                if(option.callback){
                    window.callback = option.callback
                }
                if(option.success){
                    window.success = option.success
                }
                if(option.cancel){
                    window.cancel = option.cancel
                }
                if(option.end){
                    window.end = option.end
                }
                if(option.close){
                    window.close = option.close
                }
                if(url.indexOf('http')==-1){
                    url = Config.moduleurl+'/'+url;
                }
                option.title = option.title||'页面'
                option.shadeClose = option.shadeClose||true
                option.shade = option.shade||0.8
                option.area = option.area||['99%','99%']
                option.success = option.success||null
                option.cancel = option.cancel||null
                option.end = option.end||null
                option.close = option.close||null
                option.content = url
                option.type = option.type||2
                layer.open(option)

            }
            return {
                handleShowAttachment,
                handleRemove,
                handleFileSuccess,
                changeUrl,
                showStr
            }
        }
    }
    return mufiles
})